package com.lc.cehui.ui.fragment;

import android.graphics.Paint;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.request.RequestOptions;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.hjq.http.EasyHttp;
import com.hjq.http.listener.HttpCallback;
import com.lc.cehui.R;
import com.lc.cehui.app.AppFragment;
import com.lc.cehui.http.glide.GlideApp;
import com.lc.cehui.http.request.MyCollectApi;
import com.lc.cehui.http.response.CollectListBean;
import com.lc.cehui.other.IntentKey;
import com.lc.cehui.ui.activity.mine.CollectionActivity;
import com.lc.cehui.util.SPUtil;
import com.scwang.smart.refresh.header.MaterialHeader;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;

public class CollectionShopFragment extends AppFragment<CollectionActivity> {
    private static final String ARG_PARAM1 = "param1";
    @BindView(R.id.addr_recycle)
    RecyclerView addrRecycle;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    private String system_id;
    private List<CollectListBean.DataBean> dataList = new ArrayList<>();
     private  int mPage=1;
    private String flag;
    private BaseQuickAdapter adpter;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_collectionshop;
    }

    @Override
    protected void initView() {
        my_collect_list();
    }

    private void my_collect_list() {
        EasyHttp.post(this)
                .api(new MyCollectApi().setPage(mPage).setUserid(SPUtil.get(getContext(), IntentKey.USERID,0)).setType(1))
                .request(new HttpCallback<CollectListBean>(this) {

                    @Override
                    public void onSucceed(CollectListBean data) {

                        if (data.getCode().equals("200")) {
                            flag = data.getFlag();
                            dataList.addAll(data.getData());
                            adpter.setNewData(dataList);
                            adpter.notifyDataSetChanged();
                        }

                    }
                });
    }

    @Override
    protected void initData() {

    }

    public static CollectionShopFragment newInstance(String system_id) {
        CollectionShopFragment fragment = new CollectionShopFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, system_id);

        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            system_id = getArguments().getString(ARG_PARAM1);

        }
    }

    @Override
    public void onViewCreated(@NonNull @NotNull View view, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        addrRecycle.setLayoutManager(new LinearLayoutManager(getContext()));
        adpter = new BaseQuickAdapter<CollectListBean.DataBean, BaseViewHolder>(R.layout.item_collectionshop, dataList) {
            @Override
            protected void convert(@NotNull BaseViewHolder baseViewHolder, CollectListBean.DataBean bean) {
                baseViewHolder.setText(R.id.tv_title, bean.getName());
                TextView tv_preprice=baseViewHolder.getView(R.id.tv_preprice);
                tv_preprice.setText("¥"+bean.getY_price());
                tv_preprice.getPaint().setFlags(Paint. STRIKE_THRU_TEXT_FLAG);

                baseViewHolder.setText(R.id.tv_price, "¥"+bean.getPrice());

                GlideApp.with(requireContext()).load(bean.getPicurl()).apply(new RequestOptions().placeholder(R.drawable.image11).error(R.drawable.image11)).into((ImageView) baseViewHolder.getView(R.id.img));

            }
        };
        addrRecycle.setAdapter(adpter);
        refreshLayout.setRefreshHeader(new MaterialHeader(getContext()));
        refreshLayout.setOnLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {

                if (flag.equals("T")) {
                    mPage++;
                    my_collect_list();
                    refreshLayout.finishLoadMore();
                } else {
                    toast("数据全部加载完毕");

                }
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                mPage = 1;
                dataList.clear();
                my_collect_list();
                refreshLayout.finishRefresh();
            }
        });
//        refreshLayout.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
//            @Override
//            public void onLoadMore(final @NonNull RefreshLayout refreshLayout) {
//                refreshLayout.getLayout().postDelayed(() -> {
//
//                }, 1000);
//            }
//
//            @Override
//            public void onRefresh(final @NonNull RefreshLayout refreshLayout) {
//                refreshLayout.getLayout().postDelayed(() -> {
//                    mPage = 1;
//                    dataList.clear();
//                    my_collect_list();
//                    refreshLayout.finishRefresh();
//                }, 1000);
//
//            }
//        });
    }




}
